﻿using Database.Exceptions;
using GameLogic;
using GameLogic.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace GameAPI.Controllers
{
    /// <summary>
    /// Handles the meta operations of a game, such as Init, Starting 
    /// and Joining of a Game by a Character
    /// </summary>
    public class GameController : ApiController
    {

        private readonly GameManager _gameManager;

        public GameController()
        {
            _gameManager = new GameManager();
        }

        public HttpResponseMessage InitGame(Guid characterId)
        {
            var islandId = _gameManager.InitGame(characterId);

            return Request.CreateResponse(HttpStatusCode.OK, islandId);
        }

        public HttpResponseMessage StartGame(Guid IslandId, Guid characterId)
        {
            // Control that Island exists
            // Control that Character exists
            // Control enough participant

            // Create Place for each Character
            // Add Character to Place

            // Return ok-message

            throw new System.NotImplementedException();
        }

        public HttpResponseMessage JoinGame(Guid islandId, Guid characterId)
        {
            if (islandId == null || islandId == Guid.Empty ||
                characterId == null || characterId == Guid.Empty)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, "Parameter(s) are wrong");
            }

            try
            {
                var result =_gameManager.JoinGame(characterId, islandId);
                if (result)
                    return Request.CreateResponse(HttpStatusCode.OK, "Character {" + characterId +"} successfully joined the Island {" + islandId + "}");
                else
                    return Request.CreateResponse(HttpStatusCode.InternalServerError, "Unknown error");
            }
            catch (EntityDoesNotExistException e)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound, "Did not find " + e.Message);
            }
            catch (ValidationFailedException e)
            {
                return Request.CreateResponse(HttpStatusCode.Forbidden, "Validation failed: " + e.Message);
            }
            catch (Exception e)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, e.Message);
            }

            
        }

        public HttpResponseMessage LeaveGame(Guid IslandId, Guid characterId)
        { 
            // Control that Island exists
            // Control that Character exists
            // Control that Character is in game
            // Control that Character is permitted to leave

            // Remove Character from Island

            // Return ok-message

            throw new System.NotImplementedException();
        }
    }
}
